package VectorApproach;

import java.util.ArrayList;

import XmlLoader.Position;
import Cluster.Vector;

public class TrajVector extends Vector {
	
	int trajID = -1, trajSegID = -1;
	Time time = null;
	double length = 0.0;
	
	public TrajVector(int posX, int posY, int xVect, int yVect, int trajID, int trajSegID, Time time) {
		super(posX, posY, xVect, yVect);
		
		this.trajID = trajID;
		this.trajSegID = trajSegID;
		this.time = time;
		
		this.length = this.startPos.ACT001_countDist(this.getEndPos());
	}
	
	public Position ACT001_predictFinalPos(Position posStart, int time) {
		double unitDist = this.getEndPos().ACT001_countDist(this.getStartPos()) / (this.time.end - this.time.start);
		double vt = Math.sqrt(unitDist*unitDist / (this.eachVector.x * this.eachVector.x + this.eachVector.y * this.eachVector.y));
		double vx = this.eachVector.x * vt;
		double vy = this.eachVector.y * vt;
		return new Position((int) (posStart.x + vx * time), (int) (posStart.y + vy * time));
	}
	
	public double ACT002_getVt(double length) {
		return Math.sqrt(length*length / (this.eachVector.x * this.eachVector.x + this.eachVector.y * this.eachVector.y));
	}
	
	public double ACT003_getLength() {
		return this.length;
	}
	
	public ArrayList<TrajVector> ACT004_split(int maxLength, int segStartId) {
		Position startPos = new Position(this.startPos.x, this.startPos.y);
		ArrayList<TrajVector> list = new ArrayList<TrajVector>();
		int segId = segStartId;
		double unit = this.ACT002_getVt(maxLength);
		double xunit = this.eachVector.x * unit;
		double yunit = this.eachVector.y * unit;
		while(startPos.ACT001_countDist(this.getEndPos()) > maxLength){
			TrajVector newSubv = new TrajVector(startPos.x, startPos.y, (int)xunit, (int)yunit, this.trajID, segId, new Time(-1,-1));
			list.add(newSubv);
			startPos.x = (int)(startPos.x + xunit);
			startPos.y = (int)(startPos.y + yunit);
			segId = segId + 1;
		}
		if(startPos.ACT001_countDist(this.getEndPos()) > 0){
			TrajVector newSubv = new TrajVector(startPos.x, startPos.y, this.getEndPos().x - startPos.x, this.getEndPos().y - startPos.y,
					this.trajID, segId, new Time(-1, -1));
			list.add(newSubv);
		}
		return list;
	}
	
	public boolean isEqual(TrajVector traj) {
		return (traj.GET001_trajID() == this.GET001_trajID()) && 
			   (traj.GET002_trajSegID() == this.GET002_trajSegID());
	}
	
	public int GET001_trajID() {
		return this.trajID;
	}
	
	public int GET002_trajSegID() {
		return this.trajSegID;
	}
	
	public Time GET003_time() {
		return this.time;
	}
}
